package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Menu;
import com.hzdl.car.service.MenuService;
import com.hzdl.car.service.RoleService;
import com.hzdl.car.util.AJAXResult;
import com.hzdl.car.util.TreeNode;
import com.hzdl.car.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {


    @Autowired
    private RoleService rs;
    @Autowired
    private MenuService ms;
    @RequestMapping("/roleInit")
    public String roleInit(){
        return "system/role";
    }
    @RequestMapping("/roleShow")
    @ResponseBody
    private AJAXResult roleShow(RoleVo roleVo){
        if(roleVo.getPage()==null){
            roleVo.setPage(1);
        }
        if(roleVo.getLimit()==null){
            roleVo.setLimit(5);
        }
        PageInfo pageInfo=rs.findByWhere(roleVo);
        return new AJAXResult(0,"",pageInfo.getList(),pageInfo.getTotal());

    }
    @RequestMapping("/addRole")
    @ResponseBody
    public AJAXResult roleAdd(RoleVo roleVo){
        System.out.println(roleVo);
        if(roleVo.getRoleid()==null){
            int row=rs.addRole(roleVo);
            if(row==0){
                return AJAXResult.ADD_ERROR;
            }
            return AJAXResult.ADD_SUCCESS;
        }else{
            int row = rs.updateRole(roleVo);
            if(row==0){
                return AJAXResult.UPDATE_ERROR;
            }
            return AJAXResult.RESET_SUCCESS;
        }
    }
    @RequestMapping("/roleDelete")
    @ResponseBody
    public AJAXResult roleDelete(RoleVo roleVo){
        int row = rs.deleteRole(roleVo);
        if(row==0){
            return AJAXResult.DELETE_ERROR;
        }
        return AJAXResult.DELETE_SUCCESS;
    }
    @RequestMapping("/delAll")
    @ResponseBody
    public AJAXResult delAll(String ids){
   String[] id= ids.split(",");
   int row=0;
   for(int i = 0;i<id.length;i++) {
       row += rs.deleteRoleMenuByRoleId(Integer.valueOf(id[i]));
   }

   if(row==0){
           return AJAXResult.DELETE_ERROR;
       }
   return AJAXResult.DELETE_SUCCESS;
    }

    @RequestMapping("/roleTreeJson")
    @ResponseBody
    public AJAXResult roleTreeJson(RoleVo roleVo){

        // 查询所有的菜单
        List<Menu> allMenuList = ms.findAll();
        List<TreeNode> allTreeNode = new ArrayList<>();
        // 查询出来的菜单集合 转换为 需要的类型TreeNode（页面需要的类型）
        for(Menu menu : allMenuList){
            TreeNode treeNode = new TreeNode(menu.getId(),menu.getPid(),
                    menu.getName(),menu.getIcon(),
                    menu.getHref(),menu.getOpen()==1?true:false);
            allTreeNode.add(treeNode);
        }
        // 根据角色id查询拥有的菜单
        List<Menu> roleMenuList = rs.findMenuByRoleid(roleVo.getRoleid());

        // 设置是 否选中
            // 获取用户的菜单id
            //  在所有菜单allTreeNode中查找，如果有就把checkArr设置为1
        for (TreeNode treeNode : allTreeNode) {
            for (Menu menu : roleMenuList
            ) {
                if (treeNode.getId() == menu.getId()) {
                    treeNode.setCheckArr("1");
                    break;
                }
            }
        }
        // 转换成层级关系
        List<TreeNode> treeNodes = getInfo(allTreeNode);

        return new AJAXResult(0,"",treeNodes);
    }



    @RequestMapping("/saveRoleMenu")
    @ResponseBody
    public AJAXResult saveRoleMenu(RoleVo roleVo){
        System.out.println(roleVo);

        int row = rs.roleMenu(roleVo);
        if(row == 0){
            return AJAXResult.DISPATCH_ERROR;
        }
        return AJAXResult.DISPATCH_SUCCESS;
    }


    private List<TreeNode> getInfo(List<TreeNode> treeNodes){
        List<TreeNode> treeNodeList = new ArrayList<>();
        // 区分一级菜单还是二级菜单
        for(TreeNode treeNode:treeNodes){
            if(treeNode.getPid()!=null && treeNode.getPid() == 1){
                treeNodeList.add(treeNode);
                continue;
            }

            for( TreeNode treeNode1:treeNodeList ){
                if(treeNode.getPid() != null && treeNode.getPid() == treeNode1.getId() ){
                    treeNode1.getChildren().add(treeNode);
                }
            }
        }

        List<TreeNode> treeNodeList2 = new ArrayList<>();
        for(TreeNode treeNode:treeNodes){
            if(treeNode.getPid() == 0){
                treeNode.setChildren(treeNodeList);
                treeNodeList2.add(treeNode);
            }
        }

        return treeNodeList2;

    }



}
